﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;

namespace Mate.Core.Enums
{
    public class EnumHelper<TEnum> where TEnum : struct
    {
        public static List<string> GetItems()
        {
            Type type = typeof(TEnum);

            List<string> enumNameList = Enum.GetNames(type).ToList<string>();
            return enumNameList;
        }


        public static List<int> GetValues()
        {
            Type type = typeof(TEnum);
            Array enumValueList = Enum.GetValues(type);

            List<int> valuesList = new List<int>();
            foreach (int value in enumValueList)
            {
                valuesList.Add(value);
            }

            return valuesList;
        }
        /// <summary>
        /// 获取项名称与描述
        /// </summary>
        /// <returns></returns>
        public static IList<KeyValuePair<string, string>> GetItemAndDes()
        {
            IList<KeyValuePair<string, string>> list = new List<KeyValuePair<string, string>>();
            Type type = typeof(TEnum);
            List<string> enumNames = GetItems();
            foreach (string enumName in enumNames)
            {
                FieldInfo info = type.GetField(enumName);
                DescriptionAttribute des = (DescriptionAttribute)Attribute.GetCustomAttribute(info, typeof(DescriptionAttribute));
                list.Add(new KeyValuePair<string, string>(enumName, (des == null ? "" : des.Description)));
            }
            return list;
        }
        /// <summary>
        /// 获取值与描述
        /// </summary>
        /// <returns></returns>
        public static IList<KeyValuePair<int, string>> GetValueAndDes()
        {
            IList<KeyValuePair<int, string>> list = new List<KeyValuePair<int, string>>();
            Type type = typeof(TEnum);
            List<string> enumNames = GetItems();
            foreach (string enumName in enumNames)
            {
                int value = (int)Enum.Parse(type, enumName, true);
                FieldInfo info = type.GetField(enumName);
                DescriptionAttribute des = (DescriptionAttribute)Attribute.GetCustomAttribute(info, typeof(DescriptionAttribute));
                list.Add(new KeyValuePair<int, string>(value, (des == null ? "" : des.Description)));
            }
            return list;
        }

        /// <summary>
        /// 获取值与描述
        /// </summary>
        /// <returns></returns>
        public static IList<KeyValuePair<string, string>> GetValueAndDes2()
        {
            IList<KeyValuePair<string, string>> list = new List<KeyValuePair<string, string>>();
            Type type = typeof(TEnum);
            List<string> enumNames = GetItems();
            foreach (string enumName in enumNames)
            {
                int value = (int)Enum.Parse(type, enumName, true);
                FieldInfo info = type.GetField(enumName);
                DescriptionAttribute des = (DescriptionAttribute)Attribute.GetCustomAttribute(info, typeof(DescriptionAttribute));
                list.Add(new KeyValuePair<string, string>(value.ToString(), (des == null ? "" : des.Description)));
            }
            return list;
        }

        /// <summary>
        /// 获取值与项名称
        /// </summary>
        /// <returns></returns>
        public static IList<KeyValuePair<int, string>> GetValueAndName()
        {
            IList<KeyValuePair<int, string>> list = new List<KeyValuePair<int, string>>();
            Type type = typeof(TEnum);
            List<string> enumNames = GetItems();
            foreach (string enumName in enumNames)
            {
                int value = (int)Enum.Parse(type, enumName, true);
                list.Add(new KeyValuePair<int, string>(value, enumName));
            }
            return list;
        }

    }
}
